<?php
include_once('../includes/definer.php');
include_once(INC.'admin/php/clases/FileImage.php');
$i = rand(99999, 999999);
$nombre = $_GET['identifier'].'-'.$i;
$ruta = INC.$_GET['ruta'];

//validaciones
$ext = explode('.', $_GET['qqfile']);
$ext = '.'.$ext[count($ext)-1];
$ext = strtolower($ext);
$ext = ($ext == '.jpeg')?'.jpg':$ext;
if ($ext != '.jpg' && $ext != '.jpeg' && $ext != '.gif' && $ext != '.png' )exit($ext);

//save images
$input = fopen("php://input", "r");
$temp = tmpfile();
stream_copy_to_stream($input, $temp);
fclose($input);
$target = fopen($ruta.$nombre.$ext, 'w');
fseek($temp, 0, SEEK_SET);
stream_copy_to_stream($temp, $target);
fclose($target);

//validacion
if (!file_exists($ruta.$nombre.$ext))exit();

$fileImages = new FileImage($ruta.$nombre.$ext);
$fileImages->escalar(1600, 900);
$fileImages->save($ruta.$nombre);

//respuesta
header("Content-type: application/json");
echo('{success:true, image:"'.$nombre.$ext.'", id:"'.$i.'", ancho:"'.$fileImages->ancho.'", alto:"'.$fileImages->alto.'"}');
?>
